/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.chart.bubble;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JComboBox;
import javax.swing.JToolBar;

import twaver.Element;
import twaver.Node;
import twaver.TDataBox;
import twaver.TWaverConst;
import twaver.chart.Bubble;
import twaver.chart.BubbleChart;
import demo.chart.Portlet;

public class WeekBubbleChart extends Portlet {
	private TDataBox box = new TDataBox();
	private Element element = new Node();
	
	private BubbleChart chart = new BubbleChart(box){
		protected String getFormatedYScaleText(double value) {
			return super.getFormatedYScaleText(value)+"%";
		}
	};

	public WeekBubbleChart() {
		super.initialize(chart);
		chart.setUnit("%");
		chart.setYScaleMaxValue(100);
		chart.setYScaleMinValue(0);
		chart.setYScaleValueGap(10);
		chart.setXScaleMaxValue(120);
		chart.setXScaleValueGap(20);
		chart.addXScaleText("Monday");
		chart.addXScaleText("Tuesday");
		chart.addXScaleText("Wednesday");
		chart.addXScaleText("Thursday");
		chart.addXScaleText("Friday");
		chart.setYScaleTextVisible(true);
		chart.getLegendPane().setVisible(false);
		chart.setBubbleAlpha(1f);
		chart.setXScaleLineVisible(true);
		chart.setValueTextVisible(false);
		chart.setShapeLineVisible(false);

		element.setName("element");
		element.putChartColor(new Color(204, 153, 0));
		box.addElement(element);
		initElement(20, 50, 15);
		initElement(40, 60, 20);
		initElement(60, 80, 30);
		initElement(80, 50, 40);
		initElement(100, 40, 10);
	}

	public JToolBar getControlPanel() {
		JToolBar toolbar = super.getControlPanel();
		final JComboBox combobox = new JComboBox();
		combobox.setPreferredSize(new Dimension(combobox.getPreferredSize().width, 20));
		combobox.addItem("horizontal");
		combobox.addItem("vertical ");
		combobox.addItem("left ");
		combobox.addItem("right ");
		combobox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if (combobox.getSelectedIndex() == 0) {
					chart.setXScaleTextOrientation(TWaverConst.LABEL_ORIENTATION_HORIZONTAL);
				} else if (combobox.getSelectedIndex() == 1) {
					chart.setXScaleTextOrientation(TWaverConst.LABEL_ORIENTATION_VERTICAL);
				} else if (combobox.getSelectedIndex() == 2) {
					chart.setXScaleTextOrientation(TWaverConst.LABEL_ORIENTATION_LEFT);
				} else if (combobox.getSelectedIndex() == 3) {
					chart.setXScaleTextOrientation(TWaverConst.LABEL_ORIENTATION_RIGHT);
				}
			}
		});
		combobox.setSelectedIndex(1);
		combobox.setPreferredSize(new Dimension(combobox.getPreferredSize().width, 20));
		toolbar.add(combobox);
		return toolbar;
	}

	
	public void initElement(double x, double y, double v) {
		Bubble bubble = new Bubble(x, y, v);
		element.addChartBubble(bubble);
	}
}